Skip to content

AWSOIDC: Collect required vpcs and its subnets for use in web UI#35930

Merged
kimlisa merged 8 commits intomasterfrom
lisa/collect-required-vpcs
Dec 22, 2023
Merged

AWSOIDC: Collect required vpcs and its subnets for use in web UI#35930
kimlisa merged 8 commits intomasterfrom
lisa/collect-required-vpcs

Conversation

@kimlisa
Copy link
Copy Markdown
Contributor

@kimlisa kimlisa commented Dec 20, 2023

part of #35434

Web UI requires to get a map of missing vpc and its subnets so that auto-deploy step can deploy services for the missing vpcs. If there are no missing vpcs, then user can skip the auto-deploy screen.

Here is overview:

  • collect all rds instances and clusters into one, and create a map of vpc and its subnets
  • collect all database services with the fargate/ecs label
    • iterate through these services and look for exact label matches for:
      • account-id
      • region
      • vpc id

@kimlisa kimlisa force-pushed the lisa/collect-required-vpcs branch from 6802164 to 0a8d2c4 Compare December 20, 2023 19:33
@kimlisa kimlisa marked this pull request as ready for review December 20, 2023 19:37
@github-actions
Copy link
Copy Markdown
Contributor

The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with changelog: followed by the changelog entries for the PR.

@kimlisa kimlisa added no-changelog Indicates that a PR does not require a changelog entry backport/branch/v14 labels Dec 20, 2023
Copy link
Copy Markdown
Contributor

@marcoandredinis marcoandredinis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still want to pass again, specially on the logic where we remove the VPCs
I think it's ok, but I need fresh eyes for this 😅

Comment thread lib/web/apiserver.go Outdated
Comment thread lib/web/integrations_awsoidc.go Outdated
Comment thread lib/web/integrations_awsoidc.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this over slack, but this might not return the expected DB Services while the deployed version is <14.2.4

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you suggesting that i remove it? (or just a FYI during testing?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI during testing.

I will try to create a dev build and maybe we can use that during our tests
It will be based on the current v14 branch which includes the new label

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can hard-code the following version if you want to check for the label

teleportVersionTag := "14.2.5-dev.marco.2" 

Example:
$ tctl get db_services

kind: db_service
metadata:
  expires: "2023-12-21T15:44:50.327704669Z"
  id: 1703172890371535865
  labels:
    teleport.dev/awsoidc-agent: "true"
  name: 05c13b7c-ed93-42de-9f43-753c1afc08f2
  revision: fba61218-27ea-45c9-ad81-3ff2384a7c1c
spec:
  resources:
  - aws: {}
    labels:
      account-id: "278576220453"
      region: us-east-1
      vpc-id: vpc-092abc
version: v1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worked great 👍

lisakim ~/gravitational/teleport/e/build [master] $ ./tctl get db_services
kind: db_service
metadata:
  expires: "2023-12-22T04:55:30Z"
  id: 1703218345695400557
  labels:
    teleport.dev/awsoidc-agent: "true"
  name: 9cdde9be-9bd5-4c12-b203-363df6059e5c
spec:
  resources:
  - aws: {}
    labels:
      account-id: "278576220453"
      region: us-east-1
      vpc-id: vpc-02149278b986b6f83
version: v1
---
kind: db_service
metadata:
  expires: "2023-12-22T04:55:14Z"
  id: 1703218339966369543
  labels:
    teleport.dev/awsoidc-agent: "true"
  name: fb7abf52-a70b-4253-849a-da7ebe1cb92d
spec:
  resources:
  - aws: {}
    labels:
      account-id: "278576220453"
      region: us-east-1
      vpc-id: vpc-092c26a0e0e802e92
version: v1

Comment thread lib/web/integrations_awsoidc.go Outdated
Comment thread lib/web/integrations_awsoidc.go Outdated
@kimlisa kimlisa force-pushed the lisa/collect-required-vpcs branch from eb56d8b to 0885e70 Compare December 20, 2023 20:51
@kimlisa kimlisa changed the title AutoDiscover: Collect required vpcs and its subnets for use in web UI AWSOIDC: Collect required vpcs and its subnets for use in web UI Dec 21, 2023
Copy link
Copy Markdown
Contributor

@marcoandredinis marcoandredinis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left some comments

Comment thread lib/integrations/awsoidc/listdatabases.go Outdated
Comment thread lib/integrations/awsoidc/listdatabases.go Outdated
Comment thread lib/web/integrations_awsoidc.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI during testing.

I will try to create a dev build and maybe we can use that during our tests
It will be based on the current v14 branch which includes the new label

Comment thread lib/web/integrations_awsoidc.go Outdated
Comment on lines 454 to 485
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do some assumptions here: we are only interested in the DatabaseServices deployed by us, which have a known configuration.

We should only have a single ResourceMatcher with non-empty Label set, and each Label must have a single LabelValue.
This should allow us to simplify the multiple for loops we have right now.

Suggested change
// Start looking for db service matches.
wantLabels := map[string][]string{
types.DiscoveryLabelAccountID: {},
types.DiscoveryLabelRegion: {},
types.DiscoveryLabelVPCID: {},
}
for _, svc := range fetchedDbSvcs {
// Create a lookup table of labels for easier matching.
labelLookup := map[string][]string{}
for _, matcher := range svc.GetResourceMatchers() {
for key, newVals := range *matcher.Labels {
if existingVals, ok := labelLookup[key]; ok {
labelLookup[key] = append(existingVals, newVals...)
continue
}
labelLookup[key] = newVals
}
}
// Do an exact match, b/c other labels may not match.
if len(labelLookup) != len(wantLabels) {
continue
}
// Match labels contains the keys we are looking for.
matchedLabelKeys := true
for key := range wantLabels {
vals, found := labelLookup[key]
if !found {
matchedLabelKeys = false
break
}
wantLabels[key] = vals
}
if matchedLabelKeys &&
slices.Contains(wantLabels[types.DiscoveryLabelAccountID], req.AccountID) &&
slices.Contains(wantLabels[types.DiscoveryLabelRegion], req.Region) {
// Delete found vpcs
vpcs := wantLabels[types.DiscoveryLabelVPCID]
for _, vpc := range vpcs {
delete(vpcLookup, vpc)
}
}
}
for _, svc := range fetchedDbSvcs {
if len(svc.GetResourceMatchers()) != 1 || svc.GetResourceMatchers()[0].Labels == nil {
continue
}
labelMatcher := *svc.GetResourceMatchers()[0].Labels
if len(labelMatcher) != 3 {
continue
}
if slices.Compare(labelMatcher[types.DiscoveryLabelAccountID], []string{req.AccountID}) != 0 {
continue
}
if slices.Compare(labelMatcher[types.DiscoveryLabelRegion], []string{req.Region}) != 0 {
continue
}
if len(labelMatcher[types.DiscoveryLabelVPCID]) != 1 {
continue
}
delete(vpcLookup, labelMatcher[types.DiscoveryLabelVPCID][0])
}

Tests are failing with this change, but that's because we have multiple ResourceMatchers on the DatabaseServices configuration we created

Comment thread lib/web/integrations_awsoidc.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add more information about what this endpoint does and where it is used.

@kimlisa kimlisa force-pushed the lisa/collect-required-vpcs branch from 0885e70 to 43e099f Compare December 21, 2023 22:46
@kimlisa kimlisa force-pushed the lisa/collect-required-vpcs branch from 43e099f to aacc1af Compare December 21, 2023 23:22
@kimlisa
Copy link
Copy Markdown
Contributor Author

kimlisa commented Dec 22, 2023

friendly ping @strideynet @EdwardDowling

Comment thread lib/web/integrations_awsoidc_test.go Outdated
Comment on lines +634 to +636
func stringPointer(s string) *string {
return &s
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅
I think we should use aws.String() instead.
I used this method in the past, but given that it was only being used for AWS Types, I converted most of them to just use the auxiliary method from the AWS package

}
}

func TestAWSOIDCRequiredVPCSHelper(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these tests be t.Parallel() ?

@kimlisa kimlisa enabled auto-merge December 22, 2023 16:11
@kimlisa kimlisa added this pull request to the merge queue Dec 22, 2023
Merged via the queue into master with commit 386c652 Dec 22, 2023
@kimlisa kimlisa deleted the lisa/collect-required-vpcs branch December 22, 2023 16:48
@public-teleport-github-review-bot
Copy link
Copy Markdown

@kimlisa See the table below for backport results.

Branch Result
branch/v14 Failed

kimlisa added a commit that referenced this pull request Dec 22, 2023
)

* Discover: collect all rds vpc ids and its subnets

* Address Cr

* Add api endpoint to web config

* Address Cr

* Fix web api route

* Address CR
github-merge-queue Bot pushed a commit that referenced this pull request Jan 8, 2024
#35930) (#36016)

* AWSOIDC: Collect required vpcs and its subnets for use in web UI (#35930)

* Discover: collect all rds vpc ids and its subnets

* Address Cr

* Add api endpoint to web config

* Address Cr

* Fix web api route

* Address CR

* Fix lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog Indicates that a PR does not require a changelog entry size/md

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants